/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import dao.PersonDao;
import entity.Person;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Persistence;

/**
 *
 * @author Edison Parra
 */
public class PersonControl {
   
    /**Constant fot the operations result. Success. 
     */
    private static final int Zero = 0;
    /**Constant fot the operations result. 
     */
    private static final  int One = -1;
    /**Constant fot the operations result. 
     */
    private static final int Two = -2;
    /**Constant fot the operations result. 
     */
    private static final int Three = -3;
    /**Constant fot the operations result. 
     */
    private static final int Four = -4;
    /**Constant fot the operations result. 
     */
    private static final int Five = -5;
    
    /**The entityManagerFactory to create the EntityManager. 
     */
   private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("CajeroAutomaticoPU");

    /**Creates a person in the DB.
     * @param person The object to be created
     * @return true or false
     */
    public int createUser(final Person person) {
        int result = verifyInfo(person);
        if (result != 0) {
            return result;
        }
        EntityManager em = emf.createEntityManager();
        PersonDao personDao = new PersonDao();
        em.getTransaction().begin();
        try {
            personDao.create(person, em);
            em.getTransaction().commit();
            return Zero;
        } catch (NoResultException ex) {
            return Four;
        } catch (NonUniqueResultException ex) {
            return Four;
        } finally {
            em.close();
        }
    }
    
    /**This method verifies that the information of a person.
     * to be registered is valid
     * @param person The object to be verified
     * @return Number identifying the reslut of the operation
     */
    public int verifyInfo(final Person person) {
        try {
            if (!verifyUsernameLength(person.getUserName())) {
                return One;
            }
            if (!verifyPasswordLength(person.getPassword())) {
                return Two;
            }
             if (existsUsername(person.getUserName())) {
                return Three;
            }
            if (!verifyDocumentLength(person.getDocument())) {
                return Four;
            }
            return Zero;
        } catch (Exception ex) {
            return Five;
        }
    }

    /**Verifies the length of the username string.
     * @param username  The string to be verified
     * @return true if the username length is correct. false otherwise
     */
    public boolean verifyUsernameLength(final String username) {
        return (username.matches(".{5,20}"));
    }

    /**True if the password length is correct. false otherwise.
     * @param password The string to be verified
     * @return boolean
     */
    public boolean verifyPasswordLength(final String password) {
        return (password.matches(".{5,30}"));
    }

    /**Return true if the username exists in the database. False otherwise.
     * @param username the Username
     * @return boolean
     */
    public boolean existsUsername(final String username) {
        PersonDao personDao = new PersonDao();
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            List<Person> persons = personDao.searchByUsername(username, em);
            return !persons.isEmpty();
        } catch (NoResultException ex) {
            return false;
        } catch (NonUniqueResultException ex) {
            return false;
        } finally {
            em.close();
        }
    }

    /**Return true if the document length is correct. false otherwise.
     * @param document The string to be verified.
     * @return true if the document length is correct. false otherwise
     */
    public boolean verifyDocumentLength(final String document) {
        return (document.matches(".{7,12}"));
    }

    /**This method updates the information of a person in the database.
     * @param username The username of the customer updating the password.
     * @param newPass The new password.
     * @return The number identifying the result of te operation
     */
    public int updatePass(final String username, final String newPass) {
        if (!verifyPasswordLength(newPass)) {
            return One;
        }
        EntityManager em = emf.createEntityManager();
        try {
            PersonDao personDao = new PersonDao();
            List<Person> persons = personDao.searchByUsername(username, em);
            Person person = persons.get(0);
            person.setPassword(newPass);
            em.getTransaction().begin();
            personDao.updatePass(person, em);
            em.getTransaction().commit();
            return Zero;
        } catch (NoResultException ex) {
            return Two;
        } catch (NonUniqueResultException ex) {
            return Two;
        } finally {
            em.close();
        }
    }
    
    /**This method delete the information of a person from the database.
     * @param username The username to search and delete the customer.
     * @return True if the operation was Successful.
     */
    public boolean delete(final String username) {
        EntityManager em = emf.createEntityManager();
        PersonDao personDao = new PersonDao();
        em.getTransaction().begin();
        try {
            List<Person> persons = personDao.searchByUsername(username, em);
            personDao.delete(persons.get(0), em);
            em.getTransaction().commit();
            return true;
        } catch (NoResultException ex) {
            return false;
        } catch (NonUniqueResultException ex) {
            return false;
        } finally {
            em.close();
        }
    }
}
